tidyverselibrary(tidyverse)library(DBI)
con <- dbConnect(RMySQL::MySQL(),
dbname = "gapminder",
host = "rsqltrain.ced04jhfjfgi.ap-northeast-1.rds.amazonaws.com",
port = 3306,
user = "trainstudent",
password = "csietrain")
gapminder <- dbReadTable(con, "gapminder")
dbDisconnect(con)## [1] TRUE
ggplot() + geom_point() ็นช่ฃฝๆฃไฝๅgapminder_2007 <- gapminder %>%
filter(year == 2007)
scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +
geom_point()scatteraes() ไธญๅ ๅ
ฅ color =scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point()scatterscatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()scatterggplot() + geom_line() ็นช่ฃฝ็ทๅgapminder_tw <- gapminder %>%
filter(country == "Taiwan")
line <- ggplot(gapminder_tw, aes(x = year, y = lifeExp)) +
geom_line()linegapminder_na <- gapminder %>%
filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
multi_lines <- ggplot(gapminder_na, aes(x = year, y = lifeExp, color = country)) +
geom_line()multi_linesggplot() + geom_histogram() ็นช่ฃฝ็ดๆนๅhist <- ggplot(gapminder_2007, aes(x = lifeExp)) +
geom_histogram(bins = 40)histggplot() + geom_boxplot() ็นช่ฃฝ็้ฌๅbox <- ggplot(gapminder_2007, aes(x = continent, y = lifeExp)) +
geom_boxplot()boxfacet_wrap()multi_hists <- ggplot(gapminder_2007, aes(x = lifeExp, fill = continent)) +
geom_histogram(bins = 20) +
facet_wrap(~continent, nrow = 2)multi_histsggplot() + geom_bar(stat = "identity")gapminder_2007_na <- gapminder_2007 %>%
filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
barv <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")barv+ coord_flip()barh <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+
coord_flip()barhgridExtra ๅฅไปถไพๅนซๅฟgrid.arrange() ๅฝๆธinstall.packages("gridExtra")
library(gridExtra)
gg1 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")
gg2 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+
coord_flip()grid.arrange(gg1, gg2, nrow = 2)
ggplotly() ๅ ๅ
ฅไบๅๆงplotly ๅฅไปถ็ ggplotly() ๅฝๆธinstall.packages("plotly")
library(plotly)
static_gg <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()ggplotly(static_gg)